package com.friends.utils.file;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.apache.tika.Tika;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * 使用Apache TikaApache Tika 获取文件MIME类型
 */
@Slf4j
public class FileTypeUtil {

    public static final String FILE_TYPE_XLS = "application/vnd.ms-excel";
    public static final String FILE_TYPE_XLSX = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
    public static final String FILE_TYPE_DOC = "application/msword";
    public static final String FILE_TYPE_DOCX = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
    public static final String FILE_TYPE_PDF = "application/pdf";
    public static final String FILE_TYPE_JSON = "application/json";
    public static final String FILE_TYPE_XML = "application/xml";
    public static final String FILE_TYPE_PNG = "image/png";
    public static final String FILE_TYPE_SVG = "image/svg";


    /**
     * 检查文件类型
     * @param file
     * @param fileType 要求的文件类型
     * @return
     */
    public static boolean checkFileType(File file, FileTypeEnum fileType) {

        switch (fileType) {
            case EXCEL: return isExcel(file);
            case WORD: return isWord(file);
            default: return true;
        }
    }


    /**
     * 判断是否excel文件
     * @return
     */
    public static boolean isExcel(File file) {

        try {
            String fileType = getFileType(file);
            if (FILE_TYPE_XLS.equals(fileType) || FILE_TYPE_XLSX.equals(fileType)) {
                return true;
            }
        } catch (IOException e) {
            log.error("使用Apache Tika检查文件类型时发生异常", e);
        }

        return false;
    }

    /**
     * 判断是否excel文件
     * @return
     */
    public static boolean isNotExcel(File file) {
        return !isExcel(file);
    }


    /**
     * 判断是否word文件
     * @return
     */
    public static boolean isWord(File file) {

        try {
            String fileType = getFileType(file);
            if (FILE_TYPE_DOC.equals(fileType) || FILE_TYPE_DOCX.equals(fileType)) {
                return true;
            }
        } catch (IOException e) {
            log.error("使用Apache Tika检查文件类型时发生异常", e);
        }

        return false;
    }

    /**
     * 判断是否word文件
     * @return
     */
    public static boolean isNotWord(File file) {
        return !isWord(file);
    }


    /**
     * 获取文件MIME类型
     * @param file
     * @return
     * @throws IOException
     */
    public static String getFileType(File file) throws IOException {
        Tika defaultTika = new Tika();
        return defaultTika.detect(file);
    }

    /**
     * MultipartFile 转 File
     *
     * @param file
     * @throws Exception
     */
    public static File multipartFileToFile(MultipartFile file) throws IOException {

        if (file == null || file.getSize() <= 0) {
            return null;
        }

        File toFile = new File(UUID.randomUUID() + "_" + file.getOriginalFilename());
        FileUtils.copyInputStreamToFile(file.getInputStream(), toFile);

        return toFile;
    }
}
